package se2;

import java.util.Vector;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

public class Server {


	private final static String SERVER_URL = "http://localhost:3500/RPC2";
	private final static String HANDLER = "PhoneServer";
	
    public static void changeCallLoad(int callLoad)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new Integer(callLoad));
    		
    		//Execute method on server
    		server.execute(HANDLER+".newCallLoad",params);
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
    }
    
    public static void changeBillingPeriod(int billingPeriod)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new Integer(billingPeriod));
    		
    		//Execute method on server
    		server.execute(HANDLER+".newBillingPeriod",params);
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
    }
    
    public static boolean invokeHardwareTest(String test)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new String(test));
    		
    		//Execute method on server
    		Boolean result;
    		result = (Boolean)server.execute(HANDLER+".conductHardwareTest",params);
    		
    		return result.booleanValue();
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
	    return false;
    }
    
    public static void activatePhone(String phoneip)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new String(phoneip));
    		
    		//Execute method on server
    		server.execute(HANDLER+".addedPhoneAccount",params);
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
    }
    
    public static void deactivatePhone(String phoneip)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new String(phoneip));
    		
    		//Execute method on server
    		server.execute(HANDLER+".deactivatePhone",params);
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
    }
    
    public static boolean updatePhone(String oldIp, int oldExt, int oldPerm, String newIp, int newExt, int newPerm)
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		params.addElement(new String(oldIp));
    		params.addElement(new Integer(oldExt));
    		params.addElement(new Integer(oldPerm));
    		params.addElement(new String(newIp));
    		params.addElement(new Integer(newExt));
    		params.addElement(new Integer(newPerm));
    		
    		//Execute method on server
    		Boolean result;
    		result = (Boolean)server.execute(HANDLER+".changePhoneAccount",params);
    		
    		return result;
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
	    return false;
    }
    
    public static boolean resetPhoneSystem()
    {
    	try
    	{
    		XmlRpcClient server = new XmlRpcClient(SERVER_URL);
    		Vector params = new Vector();
    		
    		//Execute method on server
    		Boolean result;
    		result = (Boolean)server.execute(HANDLER+".resetSystem",params);
    		
    		return result.booleanValue();
    	}
    	catch (XmlRpcException exception) {
	        System.err.println("JavaClient: XML-RPC Fault #" +
	                           Integer.toString(exception.code) + ": " +
	                           exception.toString());
	    } catch (Exception exception) {
	        System.err.println("JavaClient: " + exception.toString());
	    }
	    return false;
    }
}
